package com.samp.solr.zk;

import java.io.IOException;
import java.nio.file.Paths;
import java.util.regex.Pattern;

import org.apache.solr.cloud.ZkCLI;
import org.apache.solr.common.cloud.SolrZkClient;

/**
 * 如果想简单的操作zk，用curator比较方便，可参见curator.
 */
public class UploadConfig {
	
	
	private static void uploadConfig(String zk, String dir, String name){
		String paramString = "-cmd|upconfig|-zkhost|zk|-confdir|dir|-confname|name";
		String[] params = paramString.split("\\|");
		params[3] = zk;
		params[5] = dir;
		params[7] = name;
		try {
			ZkCLI.main(params);
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}

	private static void uploadPaus(){
		String zk = "127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183";
		String dir = "D:\\pinganGit\\kbp-solr\\src\\main\\collections\\paus\\conf";
		String name = "paus";
		uploadConfig(zk, dir,name);
	}
	
	private static void uploadSolrDotXml(){
		String zkServerAddress = "127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183";
		String solrXmlPath = "D:\\pinganGit\\kbp-se\\src\\solrhome\\";
		SolrZkClient zkClient = new SolrZkClient(zkServerAddress, 30000, 30000, () -> {});
		Pattern filenameExclusions = Pattern.compile(getRegixStr("solr.xml"));
		try {
			zkClient.uploadToZK(Paths.get(solrXmlPath), "/", filenameExclusions);
		} catch (IOException e) {
			e.printStackTrace();
		}
		zkClient.close();
	}
	
	private static String getRegixStr(String fileName){
		String fileReg = fileName.replaceAll("\\.", "\\\\\\.");
		return "^((?!"+fileReg+").)*$";
	}
	
	public static void main(String[] args) {
		uploadPaus();
		//uploadSolrDotXml();
	}

}
